function fig = fm_main(varargin)
% FM_MAIN create the main GUI for PSAT
%
% HDL = FM_MAIN()
%
%see general purpose structures for settings
%
%Author:    Federico Milano
%Date:      11-Nov-2002
%Update:    10-Feb-2003
%Update:    27-Feb-2003
%Update:    29-Jul-2003
%Version:   1.0.3
%
%E-mail:    Federico.Milano@uclm.es
%Web-site:  http://www.uclm.es/area/gsee/Web/Federico
%
% Copyright (C) 2002-2013 Federico Milano

global Settings Path File History Hdl Theme Fig

if exist('simulink') ~= 5
  fmlib = 'fm_choice(''Simulink is not available on this system'',2)';
else
  fmlib = 'fm_lib';
end

if nargin
  if ~exist('Fig'), Fig.main = 0, end
  if ~Fig.main
    Fig.main = findobj(get(0,'Children'),'FileName','fm_main');
  end
  switch varargin{1}

   case 'buttondown'

    pos = get(Fig.main,'CurrentPoint');
    if pos(1) > 0.04064 && pos(2) > 0.1152 && ...
          pos(1) < 0.2943 && pos(2) < 0.3395
      if ~exist('Hdl'), return, end
      if Hdl.status ~= 0,
        delete(Hdl.status)
        Hdl.status = 0;
      elseif strcmp(get(Fig.main,'SelectionType'),'open')
        fm_about
      end
    elseif pos(1) > 0.0406404 && pos(2) > 0.0358255 && ...
          pos(1) < 0.9618 && pos(2) < 0.0794
      if strcmp(get(Fig.main,'SelectionType'),'open')
        fm_hist
      end
    elseif pos(1) > 0.098522 && pos(2) > 0.7180 && ...
          pos(1) <  0.5751 && pos(2) < 0.7569
      if strcmp(get(Fig.main,'SelectionType'),'open')
        fm_set setpert,
      end
    elseif pos(1) > 0.098522 && pos(2) > 0.8317 && ...
          pos(1) < 0.5751 && pos(2) < 0.8706
      if strcmp(get(Fig.main,'SelectionType'),'open')
        fm_set setdata,
      end
    end

   case 'buttonmotion'

    if Settings.hostver >= 7.01
      pointer = 'hand';
    else
      pointer = 'crosshair';
    end

    pos = get(Fig.main,'CurrentPoint');
    if pos(1) > 0.04064 && pos(2) > 0.1152 && ...
          pos(1) < 0.2943 && pos(2) < 0.3395
      set(Fig.main,'Pointer',pointer);
    elseif pos(1) > 0.0406404 && pos(2) > 0.0358255 && ...
          pos(1) < 0.9618 && pos(2) < 0.0794
      set(Fig.main,'Pointer',pointer);
    elseif pos(1) > 0.098522 && pos(2) > 0.7180 && ...
          pos(1) <  0.5751 && pos(2) < 0.7569
      set(Fig.main,'Pointer',pointer);
    elseif pos(1) > 0.098522 && pos(2) > 0.8317 && ...
          pos(1) < 0.5751 && pos(2) < 0.8706
      set(Fig.main,'Pointer',pointer);
    else
      set(Fig.main,'Pointer','arrow');
    end

  end

  return

end

if Fig.main
  figure(Fig.main)
  return
end

simversion = '5.1 (R13SP1)';
allowsimconv = 1;

h0 = figure('Color',Theme.color01, ...
            'Units','normalized', ...
            'CreateFcn','Fig.main = gcf;', ...
            'DeleteFcn','fm_set delete', ...
            'FileName','fm_main', ...
            'KeyPressFcn','fm_set keypress', ...
            'MenuBar','none', ...
            'Name',['PSAT ',Settings.version], ...
            'NumberTitle','off', ...
            'PaperPosition',[18 180 576 432], ...
            'PaperType','A4', ...
            'PaperUnits','points', ...
            'Position',sizefig(0.6348,0.6276), ...
            'RendererMode','manual', ...
            'Resize','on', ...
            'Tag','Settings', ...
            'ToolBar','none', ...
            'UserData', 1, ...
            'WindowButtonDownFcn','fm_main buttondown', ...
            'WindowButtonMotionFcn','fm_main buttonmotion');

if Settings.hostver < 7.05
  set(h0,'ShareColors','on')
end

if Settings.hostver <= 7.09
  set(h0,'MinColormap', 256)
end

fm_set colormap

% Menu File/Open
h1 = uimenu('Parent',h0, ...
            'Label','File', ...
            'Tag','MenuFile');

h2 = uimenu('Parent',h1, ...
            'Label', 'Open', ...
            'Tag','FileOpen');
h3 = uimenu('Parent',h2, ...
            'Callback','fm_set setdata', ...
            'Label', 'Data File', ...
            'Tag','OpenData', ...
            'Accelerator','d');
h3 = uimenu('Parent',h2, ...
            'Callback','fm_set setpert', ...
            'Label', 'Perturbation File', ...
            'Tag','OpenPert', ...
            'Accelerator','j');
h3 = uimenu('Parent',h2, ...
            'Callback','fm_set opensys', ...
            'Label', 'Saved System', ...
            'Tag','OpenLF', ...
            'Accelerator','y');
h3 = uimenu('Parent',h2, ...
            'Callback','fm_simrep(''ViewModel'',0,0)', ...
            'Label', 'Current Simulink model', ...
            'Tag','OpenSimModel', ...
            'Accelerator','0');
h3 = uimenu('Parent',h2, ...
            'Callback','fm_set opensim', ...
            'Label', 'Simulink Model', ...
            'Tag','OpenSim', ...
            'Accelerator','%');

% Menu File/Save
h2 = uimenu('Parent',h1, ...
            'Label', 'Save', ...
            'Tag','FileSave');
h3 = uimenu('Parent',h2, ...
            'Callback','fm_set savesys', ...
            'Label', 'Current System', ...
            'Tag','SaveLF', ...
            'Accelerator','a');
h3 = uimenu('Parent',h2, ...
            'Callback','fm_set savedata', ...
            'Label', 'Restore Data File', ...
            'Tag','SaveD', ...
            'Accelerator','r');
h3 = uimenu('Parent',h2, ...
            'Callback','fm_dump', ...
            'Label', 'Dump Data File', ...
            'Tag','SaveDump');
h3 = uimenu('Parent',h2, ...
            'Callback','fm_set appendV normal', ...
            'Label', 'Save Voltages', ...
            'Tag','SaveV');
h3 = uimenu('Parent',h2, ...
            'Callback','fm_set appendV flat', ...
            'Label', 'Set Flat Voltages', ...
            'Tag','FlatV');
h3 = uimenu('Parent',h2, ...
            'Callback','fm_set savesettings', ...
            'Label', 'Settings', ...
            'Tag','SaveS');
h3 = uimenu('Parent',h2, ...
            'Callback','fm_simsave(''fm_lib.mdl'',Path.psat)', ...
            'Label', ['Library as Simulink ', simversion], ...
            'Tag','SaveS');
if ~allowsimconv, set(h3,'Enable','off'), end
h3 = uimenu('Parent',h2, ...
            'Callback','fm_simsave', ...
            'Label',['Save model as Simulink ',simversion], ...
            'Tag','ToolsSIMMOD');
if ~allowsimconv, set(h3,'Enable','off'), end
h3 = uimenu('Parent',h2, ...
            'Callback','fm_simsave(''all'')', ...
            'Label',['Save all models in folder as Sim. ',simversion], ...
            'Tag','ToolsALLSIM');
if ~allowsimconv, set(h3,'Enable','off'), end
h2 = uimenu('Parent',h1, ...
            'Callback','fm_set closedata', ...
            'Label','Discard Data File', ...
            'Tag','DataClose', ...
            'Separator','on');
h2 = uimenu('Parent',h1, ...
            'Callback','fm_set closepert', ...
            'Label','Discard Perturbation File', ...
            'Tag','PertClose');
h2 = uimenu('Parent',h1, ...
            'Callback','close(gcf)', ...
            'Label','Close', ...
            'Tag','FileClose', ...
            'Separator','on', ...
            'Accelerator','q');
h2 = uimenu('Parent',h1, ...
            'Callback','fm_set exit', ...
            'Label','Exit', ...
            'Tag','NetSett', ...
            'Accelerator','x');

% Menu Edit
h1 = uimenu('Parent',h0, ...
            'Label','Edit', ...
            'Tag','MenuEdit');
h2 = uimenu('Parent',h1, ...
            'Callback',fmlib, ...
            'Label', 'Simulink Library', ...
            'Tag','NetEdit', ...
            'Accelerator','s');
a = dir([Path.psat,filesep,'*.mdl']);
a = {a.name}';
idx = strmatch('fm_xlib',a);
for i = 2:length(idx)
  h2 = uimenu('Parent',h1, ...
	      'Callback',strrep(a{idx(i)},'.mdl',''), ...
	      'Label',['Extra Library #',num2str(i-1)], ...
	      'Tag',['NetEdit',num2str(i)]);
end
h2 = uimenu('Parent',h1, ...
            'Callback','fm_simset', ...
            'Label','Simulink Model Settings', ...
            'Tag','NetSett');
h2 = uimenu('Parent',h1, ...
            'Callback','fm_setting', ...
            'Label','General Settings', ...
            'Tag','ToolSett', ...
            'Separator','on', ...
            'Accelerator','k');
h2 = uimenu('Parent',h1, ...
            'Callback','fm_advanced', ...
            'Label','Advanced Settings', ...
            'Tag','ToolAdvanced');
h2 = uimenu('Parent',h1, ...
            'Callback','fm_plotsel', ...
            'Label','Select Plot Variables', ...
            'Tag','PlotSetVar');
h2 = uimenu('Parent',h1, ...
            'Callback','fm_snbfig', ...
            'Label','SNB Settings', ...
            'Tag','ToolSNBSett', ...
            'Accelerator','g');
h2 = uimenu('Parent',h1, ...
            'Callback','fm_libfig', ...
            'Label', 'LIB Settings', ...
            'Tag','ToolLIBSett', ...
            'Accelerator','l');
h2 = uimenu('Parent',h1, ...
            'Callback','fm_cpffig', ...
            'Label','CPF Settings', ...
            'Tag','ToolOPFSett', ...
            'Accelerator','i');
h2 = uimenu('Parent',h1, ...
            'Callback','fm_opffig', ...
            'Label','OPF Settings', ...
            'Tag','ToolCPFSett', ...
            'Accelerator','z');
h2 = uimenu('Parent',h1, ...
            'Callback','fm_set setdefault', ...
            'Label','Set Default', ...
            'Tag','ToolDef');

% Menu Run
h1 = uimenu('Parent',h0, ...
            'Label','Run', ...
            'Tag','MenuRun');
h2 = uimenu('Parent',h1, ...
            'Callback','fm_set lf', ...
            'Label','Power Flow', ...
            'Tag','RunPF', ...
            'Accelerator','p');
h2 = uimenu('Parent',h1, ...
            'Callback','fm_snb', ...
            'Label','Saddle-Node Bifurcation', ...
            'Tag','ViewSNB');
h2 = uimenu('Parent',h1, ...
            'Callback','fm_limit', ...
            'Label','Limit-Induced Bifurcation', ...
            'Tag','ViewLIB');
h2 = uimenu('Parent',h1, ...
            'Callback','fm_cpf(''cpf'');', ...
            'Label','Continuation Power Flow', ...
            'Tag','ViewCPF', ...
            'Accelerator','c');
h2 = uimenu('Parent',h1, ...
            'Callback','fm_set opf', ...
            'Label','Optimal Power Flow', ...
            'Tag','ViewOPF', ...
            'Accelerator','o');
h2 = uimenu('Parent',h1, ...
            'Callback','fm_n1cont', ...
            'Label','N-1 Contingency Analysis', ...
            'Tag','ViewN1C');
h2 = uimenu('Parent',h1, ...
            'Callback','fm_eigfig', ...
            'Label','Eigenvalue Analysis', ...
            'Tag','ViewEIG', ...
            'Separator','on', ...
            'Accelerator','e');
h2 = uimenu('Parent',h1, ...
            'Callback','fm_int', ...
            'Label','Time Simulation', ...
            'Tag','RunTime', ...
            'Accelerator','t');

% Menu Tools
h1 = uimenu('Parent',h0, ...
            'Label','Tools', ...
            'Tag','Tool');
h2 = uimenu('Parent',h1, ...
            'Callback','fm_dir(0)', ...
            'Label','Data Format Conversion', ...
            'Tag','ToolDir', ...
            'Accelerator','f');
h2 = uimenu('Parent',h1, ...
            'Callback','fm_comp copen', ...
            'Label','Component Browser', ...
            'Tag','ToolComp', ...
            'Accelerator','b');
h2 = uimenu('Parent',h1, ...
            'Callback','fm_snap', ...
            'Label','Snapshots', ...
            'Tag','ToolSnap', ...
            'Accelerator','n');
h2 = uimenu('Parent',h1, ...
            'Callback','fm_pmufig', ...
            'Label','PMU placement', ...
            'Tag','ToolsPMU', ...
            'Accelerator','u');
%h2 = uimenu('Parent',h1, ...
%            'Callback','fm_pcode', ...
%            'Label','Archive (p-code)', ...
%            'Tag','ToolsPCODE', ...
%            'Separator','on');
h2 = uimenu('Parent',h1, ...
            'Callback','fm_laprint', ...
            'Label','LaTeX Print', ...
            'Tag','ToolsLaPrint', ...
            'Separator','on');

% Menu Interfaces
h1 = uimenu('Parent',h0, ...
            'Label','Interfaces', ...
            'Tag','Interface');
h2 = uimenu('Parent',h1, ...
            'Callback','fm_gamsfig', ...
            'Label','GAMS', ...
            'Accelerator', '1', ...
            'Tag','InterGams');
h2 = uimenu('Parent',h1, ...
            'Callback','fm_uwfig', ...
            'Label','UWPFLOW', ...
            'Accelerator','2', ...
            'Tag','InterGams');
h2 = uimenu('Parent',h1, ...
            'Callback','fm_equivfig', ...
            'Label','Network Equivalents', ...
            'Separator','on', ...
            'Accelerator','3', ...
            'Tag','LibEquiv');

% Menu View
h1 = uimenu('Parent',h0, ...
            'Label','View', ...
            'Tag','View');
h2 = uimenu('Parent',h1, ...
            'Callback','fm_stat', ...
            'Label','Static Report', ...
            'Tag','ToolStat', ...
            'Accelerator','v');
h2 = uimenu('Parent',h1, ...
            'Callback','fm_threed', ...
            'Label','Network Visualization', ...
            'Tag','ToolStat', ...
            'Accelerator','$');
h2 = uimenu('Parent',h1, ...
            'Callback','fm_matrx', ...
            'Label','Sparse Matrix Visualisation', ...
            'Tag','ViewSparseMenu', ...
            'Accelerator','m');
%h2 = uimenu('Parent',h1, ...
%            'Callback','fm_vstab', ...
%            'Label','View Node Sensitivity', ...
%            'Tag','ViewEA');
h2 = uimenu('Parent',h1, ...
            'Callback','fm_plotfig', ...
            'Label','Plotting Utilities', ...
            'Tag','ViewPlot', ...
            'Separator','on', ...
            'Accelerator','w');
h2 = uimenu('Parent',h1, ...
            'Callback','vwplot(Wind)', ...
            'Label','Plot wind speeds', ...
            'Tag','ViewWind');

% Menu Options
h1 = uimenu('Parent',h0, ...
            'Label','Options', ...
            'Tag','Options');
h2 = uimenu('Parent',h1, ...
            'Callback','fm_hist', ...
            'Label','History', ...
            'Tag','ToolHist', ...
            'Accelerator','h');
h2 = uimenu('Parent',h1, ...
            'Callback','fm_themefig', ...
            'Label','Themes', ...
            'Tag','OptionsTh', ...
            'Separator','on');
h2 = uimenu('Parent',h1, ...
            'Callback','fm_tviewer', ...
            'Label','Text Viewer', ...
            'Tag','OptionsTV');

% Menu Help
h1 = uimenu('Parent',h0, ...
            'Label','Help', ...
            'Tag','uimenu2');
h2 = uimenu('Parent',h1, ...
            'Callback','fm_about', ...
            'Label','About PSAT', ...
            'Tag','Subuimenu3');
h2 = uimenu('Parent',h1, ...
            'Callback','web(''http://www.uclm.es/area/gsee/Web/Federico'');', ...
            'Label','PSAT website', ...
            'Tag','UWLink', ...
	    'Separator','on');
h2 = uimenu('Parent',h1, ...
            'Callback','web(''http://groups.yahoo.com/group/psatforum'');', ...
            'Label','PSAT Forum', ...
            'Tag','YahooLink');
h2 = uimenu('Parent',h1, ...
            'Label','Feedback', ...
            'Tag','MailLink');
h3 = uimenu('Parent',h2, ...
            'Callback','web(''mailto:Federico.Milano@uclm.es'');', ...
            'Label','Federico.Milano@uclm.es', ...
            'Tag','MailUW');
h3 = uimenu('Parent',h2, ...
            'Callback','web(''mailto:fmilano@ind-cr.uclm.es'');', ...
            'Label','fmilano@ind-cr.uclm.es', ...
            'Tag','MailUCLM');
h3 = uimenu('Parent',h2, ...
            'Callback','web(''mailto:psatforum@yahoo.com'');', ...
            'Label','psatforum@yahoo.com', ...
            'Tag','MailYahoo');
h2 = uimenu('Parent',h1, ...
            'Callback','fm_enter(Path.psat,Settings.version,Settings.date)', ...
            'Label','Splash Window', ...
            'Tag','SplashPSAT');

h2 = uimenu('Parent',h1, ...
            'Callback','fm_clock', ...
            'Label','Clock', ...
            'Tag','ClockMenu', ...
            'Separator','on');
h2 = uimenu('Parent',h1, ...
            'Callback','fm_warranty', ...
            'Label','No Warranty', ...
            'Tag','WarMenu', ...
            'Separator','on');
h2 = uimenu('Parent',h1, ...
            'Callback','fm_license', ...
            'Label','GNU License', ...
            'Tag','LicMenu');

% general pushbuttons
h1 = uicontrol('Parent',h0, ...
               'Units','normalized', ...
               'BackgroundColor',Theme.color03, ...
               'Callback','fm_set lf', ...
               'FontWeight','bold', ...
               'ForegroundColor',Theme.color09, ...
               'Position',[0.35345 0.26324 0.16379 0.068536], ...
               'String','Power Flow', ...
               'Tag','Pushbutton1');
h1 = uicontrol('Parent',h0, ...
               'Units','normalized', ...
               'BackgroundColor',Theme.color02, ...
               'Callback','fm_setting', ...
               'Position',[0.7968 0.26324 0.16379 0.068536], ...
               'String','Settings', ...
               'Tag','Pushbutton5');
h1 = uicontrol('Parent',h0, ...
               'Units','normalized', ...
               'BackgroundColor',Theme.color02, ...
               'Callback','fm_set opensys', ...
               'Position',[0.57512 0.18847 0.16379 0.068536], ...
               'String','Load System', ...
               'Tag','Pushbutton4', ...
               'UserData','[ ]');
h1 = uicontrol('Parent',h0, ...
               'Units','normalized', ...
               'BackgroundColor',Theme.color02, ...
               'Callback','fm_cpf(''main'');', ...
               'Position',[0.35345 0.18847 0.16379 0.068536], ...
               'String','CPF', ...
               'Tag','Pushbutton3');
h1 = uicontrol('Parent',h0, ...
               'Units','normalized', ...
               'BackgroundColor',Theme.color03, ...
               'Callback','fm_int', ...
               'FontWeight','bold', ...
               'ForegroundColor',Theme.color09, ...
               'Position',[0.57389 0.26324 0.16379 0.068536], ...
               'String','Time Domain', ...
               'Tag','Pushbutton2');
h1 = uicontrol('Parent',h0, ...
               'Units','normalized', ...
               'BackgroundColor',Theme.color02, ...
               'Callback','fm_plotfig', ...
               'Position',[0.7968 0.18847 0.16379 0.068536], ...
               'String','Plot', ...
               'Tag','Pushbutton6');
h1 = uicontrol('Parent',h0, ...
               'Units','normalized', ...
               'BackgroundColor',Theme.color02, ...
               'Callback','fm_set close', ...
               'Position',[0.7968 0.11371 0.16379 0.068536], ...
               'String','Close', ...
               'Tag','PushClose');
h1 = uicontrol('Parent',h0, ...
               'Units','normalized', ...
               'BackgroundColor',Theme.color02, ...
               'Callback','fm_set savesys', ...
               'Position',[0.57512 0.11371 0.16379 0.068536], ...
               'String','Save System', ...
               'Tag','Pushbutton10');
h1 = uicontrol('Parent',h0, ...
               'Units','normalized', ...
               'BackgroundColor',Theme.color02, ...
               'Callback','fm_set opf', ...
               'Position',[0.35345 0.11371 0.16379 0.068536], ...
               'String','OPF', ...
               'Tag','Pushbutton9');

% PSAT logo
set(Fig.main,'Units','pixels')
figdim = get(Fig.main,'Position');
set(Fig.main,'Units','normalized')
xdim = fix(0.2536946*figdim(3));
ydim = fix(0.2242991*figdim(4));

h1 = axes('Parent',h0, ...
          'CreateFcn','Hdl.axes = gca;', ...
          'Units','normalized', ...
          'Box','on', ...
          'CameraUpVector',[0 1 0], ...
          'CameraUpVectorMode','manual', ...
          'Color',Theme.color04, ...
          'ColorOrder',Settings.color, ...
          'HandleVisibility','on', ...
          'HitTest','off', ...
          'Layer','top', ...
          'Position',[0.0406404 0.1152265 0.2536946 0.22429907], ...
          'Tag','Axes1', ...
          'XColor',Theme.color03, ...
          'XLim',[0.5 xdim+0.5], ...
          'XLimMode','manual', ...
          'XTickLabelMode','manual', ...
          'XTickMode','manual', ...
          'YColor',Theme.color03, ...
          'YDir','reverse', ...
          'YLim',[0.5 ydim+0.5], ...
          'YLimMode','manual', ...
          'YTickLabelMode','manual', ...
          'YTickMode','manual', ...
          'ZColor',[0 0 0]);

a = imread([Path.images,'main_logo.jpg'],'jpg');
%a = fm_mat('main_logo');

try
  if Settings.hostver >= 7.04
    a = imresize(a,[ydim xdim],'bilinear');
  else
    a = imresize(a,[ydim xdim],'bilinear',11);
  end
catch
  % imresize is not available!!!
end
if ispc, set(h1,'XColor',[126 157 185]/255,'YColor',[126 157 185]/255), end
h2 = image( ...
    'Parent',h1, ...
    'CData',a, ...
    'Erase','none', ...
    'Tag','Axes1Image1', ...
    'XData',[1 xdim], ...
    'YData',[1 ydim]);
color1 = [0 0 0];
color2 = [0 0.503 0];
if sum(Theme.color02) < 1
  color1 = [1 1 1];
  color2 = [0 1 0];
end

h1 = text(0.08*xdim,0.175*ydim,'PSAT');
set(h1, ...
    'Color',color1, ...
    'FontSize',12, ...
    'FontWeight','bold', ...
    'FontName','Times')
h1 = text(0.08*xdim,0.9*ydim,Settings.date);
set(h1, ...
    'Color',color2, ...
    'FontSize', 9, ...
    'FontWeight','bold', ...
    'FontName','Times')
h1 = text(0.08*xdim,0.775*ydim,['Version ',Settings.version]);
set(h1, ...
    'Color',color2, ...
    'FontSize', 9, ...
    'FontWeight','bold', ...
    'FontName','Times')

% frame and general settings
h1 = uicontrol('Parent',h0, ...
               'Units','normalized', ...
               'BackgroundColor',Theme.color02, ...
               'ForegroundColor',Theme.color03, ...
               'Position',[0.04064 0.35358 0.91872 0.57125], ...
               'Enable', 'inactive', ...
               'Style','frame', ...
               'Tag','Frame1');
%h1 = uicontrol('Parent',h0, ...
%	'Units','normalized', ...
%	'BackgroundColor',Theme.color02, ...
%	'Callback','fm_set setpert', ...
%	'Position',[0.41749     0.74143-0.025     0.15764    0.042056], ...
%	'String','Set Perturbation File', ...
%	'Tag','Pushbutton8');
%h1 = uicontrol('Parent',h0, ...
%	'Units','normalized', ...
%	'BackgroundColor',Theme.color02, ...
%	'Callback','fm_set setdata', ...
%	'Position',[0.41749     0.85514-0.025     0.15764    0.042056], ...
%	'String','Set Data File', ...
%	'Tag','Pushbutton7');
h1 = uicontrol('Parent',h0, ...
               'Units','normalized', ...
               'BackgroundColor',Theme.color02, ...
               'HorizontalAlignment','left', ...
               'Position',[0.098522 0.7601 0.18547 0.028037], ...
               'String','Perturbation File', ...
               'Style','text', ...
               'Tag','StaticText10');
h1 = uicontrol('Parent',h0, ...
               'Units','normalized', ...
               'BackgroundColor',Theme.color02, ...
               'HorizontalAlignment','left', ...
               'Position',[0.098522 0.8738 0.13547 0.028037], ...
               'String','Data File', ...
               'Style','text', ...
               'Tag','StaticText9');

h1 = uicontrol('Parent',h0, ...
               'Units','normalized', ...
               'BackgroundColor',Theme.color04, ...
               'ForegroundColor',Theme.color03, ...
               'Enable', 'inactive', ...
               'Position',[0.098522 0.7180 0.4766 0.038941], ...
               'Style','frame', ...
               'Tag','FramePert');
if ispc, set(h1,'Style','edit'), end
h1 = uicontrol('Parent',h0, ...
               'Units','normalized', ...
               'BackgroundColor',Theme.color04, ...
               'ForegroundColor',Theme.color03, ...
               'Enable', 'inactive', ...
               'Position',[0.098522 0.8317 0.4766 0.038941], ...
               'Style','frame', ...
               'Tag','FrameData');
if ispc, set(h1,'Style','edit'), end

h1 = uicontrol('Parent',h0, ...
               'Units','normalized', ...
               'BackgroundColor',Theme.color04, ...
               'CreateFcn','if ~isempty(File.pert) && ~strcmp(File.pert,''pert''), set(gcbo,''String'',File.pert); end', ...
               'FontName',Theme.font01, ...
               'ForegroundColor',Theme.color07, ...
               'Enable', 'inactive', ...
               'HorizontalAlignment','left', ...
               'Position',[0.105 0.722 0.46 0.03], ...
               'Style','text', ...
               'TooltipString',[Path.pert,File.pert], ...
               'Tag','EditText10');
if strcmp('@ ',File.pert(1:min(end,2)))
  set(h1,'ForegroundColor',[0 0 0.592])
end

h1 = uicontrol('Parent',h0, ...
               'Units','normalized', ...
               'BackgroundColor',Theme.color04, ...
               'CreateFcn','if ~isempty(File.data), set(gcbo,''String'',File.data); end', ...
               'Enable', 'inactive', ...
               'FontName',Theme.font01, ...
               'ForegroundColor',Theme.color07, ...
               'HorizontalAlignment','left', ...
               'TooltipString',[Path.data,File.data], ...
               'Position',[0.105 0.8357 0.46 0.03], ...
               'Style','text', ...
               'Tag','EditText9');
if ~isempty(findstr(File.data,'(mdl)'))
  set(h1,'ForegroundColor',[0 0.592 0])
end
if strcmp('@ ',File.data(1:min(end,2)))
  set(h1,'ForegroundColor',[0 0 0.592])
end

if strcmp(Settings.platform,'MAC')
  dm = 0.01;
  aligntxt = 'center';
else
  dm = 0;
  aligntxt = 'left';
end

h1 = uicontrol('Parent',h0, ...
               'Units','normalized', ...
               'BackgroundColor',Theme.color02, ...
               'HorizontalAlignment','left', ...
               'Position',[0.098522 0.6385 0.18547 0.028037], ...
               'String','Command Line', ...
               'Style','text', ...
               'Tag','StaticText10');
h1 = uicontrol('Parent',h0, ...
               'Units','normalized', ...
               'BackgroundColor',Theme.color04, ...
               'Callback','fm_set command', ...
               'FontName',Theme.font01, ...
               'ForegroundColor',Theme.color05, ...
               'HorizontalAlignment',aligntxt, ...
               'Position',[0.098522 0.5965-dm 0.4766 0.042056+dm], ...
               'String','', ...
               'Style','edit', ...
               'Tag','EditCommand');
h1 = uicontrol('Parent',h0, ...
               'Units','normalized', ...
               'Callback','fm_set listcommand', ...
               'BackgroundColor',Theme.color01, ...
               'FontName',Theme.font01, ...
               'ForegroundColor',Theme.color05, ...
               'Position',[0.098522 0.3940 0.4766 0.17134], ...
               'String',{'<empty>'}, ...
               'Style','listbox', ...
               'Tag','ListCommand', ...
               'Value',1);

% system parameters
h1 = uicontrol('Parent',h0, ...
               'Units','normalized', ...
               'BackgroundColor',Theme.color02, ...
               'HorizontalAlignment','left', ...
               'Position',[0.79926 0.3940 0.14778 0.037383], ...
               'String','Max Dyn. Iter.', ...
               'Style','text', ...
               'Tag','StaticText8');
h1 = uicontrol('Parent',h0, ...
               'Units','normalized', ...
               'BackgroundColor',Theme.color04, ...
               'Callback','Settings.dyntol = fval(gcbo,Settings.dyntol);', ...
               'FontName',Theme.font01, ...
               'ForegroundColor',Theme.color05, ...
               'HorizontalAlignment',aligntxt, ...
               'Position',[0.69089 0.4563 0.096059 0.038941+dm], ...
               'String',num2str(Settings.dyntol), ...
               'Style','edit', ...
               'Tag','EditText7');
h1 = uicontrol('Parent',h0, ...
               'Units','normalized', ...
               'BackgroundColor',Theme.color02, ...
               'HorizontalAlignment','left', ...
               'Position',[0.79926 0.4563 0.14778 0.037383], ...
               'String','Dyn. Tolerance', ...
               'Style','text', ...
               'Tag','StaticText7');
h1 = uicontrol('Parent',h0, ...
               'Units','normalized', ...
               'BackgroundColor',Theme.color04, ...
               'Callback','Settings.dynmit = fval(gcbo,Settings.dynmit);', ...
               'FontName',Theme.font01, ...
               'ForegroundColor',Theme.color05, ...
               'HorizontalAlignment',aligntxt, ...
               'Position',[0.69089 0.3940 0.096059 0.038941+dm], ...
               'String',num2str(Settings.dynmit), ...
               'Style','edit', ...
               'Tag','EditText8');
h1 = uicontrol('Parent',h0, ...
               'Units','normalized', ...
               'BackgroundColor',Theme.color02, ...
               'HorizontalAlignment','left', ...
               'Position',[0.79926 0.5186 0.14778 0.037383], ...
               'String','Max PF Iter.', ...
               'Style','text', ...
               'Tag','StaticText6');
h1 = uicontrol('Parent',h0, ...
               'Units','normalized', ...
               'BackgroundColor',Theme.color04, ...
               'Callback','Settings.lfmit = fval(gcbo,Settings.lfmit);', ...
               'FontName',Theme.font01, ...
               'ForegroundColor',Theme.color05, ...
               'HorizontalAlignment',aligntxt, ...
               'Position',[0.69089 0.5186 0.096059 0.038941+dm], ...
               'String',num2str(Settings.lfmit), ...
               'Style','edit', ...
               'Tag','EditText6');
h1 = uicontrol('Parent',h0, ...
               'Units','normalized', ...
               'BackgroundColor',Theme.color02, ...
               'HorizontalAlignment','left', ...
               'Position',[0.79926 0.5809 0.14778 0.037383], ...
               'String','PF Tolerance', ...
               'Style','text', ...
               'Tag','StaticText5');
h1 = uicontrol('Parent',h0, ...
               'Units','normalized', ...
               'BackgroundColor',Theme.color04, ...
               'Callback','Settings.lftol = fval(gcbo,Settings.lftol);', ...
               'FontName',Theme.font01, ...
               'ForegroundColor',Theme.color05, ...
               'HorizontalAlignment',aligntxt, ...
               'Position',[0.69089 0.5809 0.096059 0.038941+dm], ...
               'String',num2str(Settings.lftol), ...
               'Style','edit', ...
               'Tag','EditText5');
h1 = uicontrol('Parent',h0, ...
               'Units','normalized', ...
               'BackgroundColor',Theme.color02, ...
               'HorizontalAlignment','left', ...
               'Position',[0.79926 0.6432 0.15778 0.037383], ...
               'String','Ending Time (s)', ...
               'Style','text', ...
               'Tag','StaticText4');
h1 = uicontrol('Parent',h0, ...
               'Units','normalized', ...
               'BackgroundColor',Theme.color04, ...
               'Callback','Settings.tf = fval(gcbo,Settings.tf);', ...
               'FontName',Theme.font01, ...
               'ForegroundColor',Theme.color05, ...
               'HorizontalAlignment',aligntxt, ...
               'Position',[0.69089 0.6432 0.096059 0.038941+dm], ...
               'String',num2str(Settings.tf), ...
               'Style','edit', ...
               'Tag','EditText4');
h1 = uicontrol('Parent',h0, ...
               'Units','normalized', ...
               'BackgroundColor',Theme.color02, ...
               'HorizontalAlignment','left', ...
               'Position',[0.79926 0.7055 0.16 0.037383], ...
               'String','Starting Time (s)', ...
               'Style','text', ...
               'Tag','StaticText3');
h1 = uicontrol('Parent',h0, ...
               'Units','normalized', ...
               'BackgroundColor',Theme.color04, ...
               'Callback','Settings.t0 = fval(gcbo,Settings.t0);', ...
               'FontName',Theme.font01, ...
               'ForegroundColor',Theme.color05, ...
               'HitTest','off', ...
               'HorizontalAlignment',aligntxt, ...
               'ListboxTop',0, ...
               'Position',[0.69089 0.7055 0.096059 0.038941+dm], ...
               'String',num2str(Settings.t0), ...
               'Style','edit', ...
               'Tag','EditText3');
h1 = uicontrol('Parent',h0, ...
               'Units','normalized', ...
               'BackgroundColor',Theme.color02, ...
               'HorizontalAlignment','left', ...
               'Position',[0.79926 0.7678 0.17 0.037383], ...
               'String','Power Base (MVA)', ...
               'Style','text', ...
               'Tag','StaticText2');
h1 = uicontrol('Parent',h0, ...
               'Units','normalized', ...
               'BackgroundColor',Theme.color04, ...
               'Callback','Settings.mva = fval(gcbo,Settings.mva,2);', ...
               'FontName',Theme.font01, ...
               'ForegroundColor',Theme.color05, ...
               'HorizontalAlignment',aligntxt, ...
               'Position',[0.69089 0.7678 0.096059 0.038941+dm], ...
               'String',num2str(Settings.mva), ...
               'Style','edit', ...
               'Tag','EditText2');
h1 = uicontrol('Parent',h0, ...
               'Units','normalized', ...
               'BackgroundColor',Theme.color02, ...
               'HorizontalAlignment','left', ...
               'Position',[0.79926 0.8301 0.14778 0.037383], ...
               'String','Freq. Base (Hz)', ...
               'Style','text', ...
               'Tag','StaticText1');
h1 = uicontrol('Parent',h0, ...
               'Units','normalized', ...
               'BackgroundColor',Theme.color04, ...
               'Callback','Settings.freq = fval(gcbo,Settings.freq,2);', ...
               'FontName',Theme.font01, ...
               'ForegroundColor',Theme.color05, ...
               'HorizontalAlignment',aligntxt, ...
               'Position',[0.69089 0.8301 0.094828 0.038941+dm], ...
               'String',num2str(Settings.freq), ...
               'Style','edit', ...
               'Tag','EditText1');

% Menubar
h1 = uicontrol('Parent',h0, ...
               'CData',fm_mat('main_open'), ...
               'Units','normalized', ...
               'BackgroundColor',Theme.color02, ...
               'Callback','fm_set setdata', ...
               'Position',[0.0379 0.9375 0.0379 0.05], ...
               'TooltipString','Open Data File', ...
               'Tag','Pushbutton7');
h1 = uicontrol('Parent',h0, ...
               'CData',fm_mat('main_opensys'), ...
               'Units','normalized', ...
               'BackgroundColor',Theme.color02, ...
               'Callback','fm_set opensys', ...
               'Position',[0.0379*2 0.9375 0.0379 0.05], ...
               'TooltipString','Open System', ...
               'Tag','Pushbutton4');
h1 = uicontrol('Parent',h0, ...
               'CData',fm_mat('main_pert'), ...
               'Units','normalized', ...
               'BackgroundColor',Theme.color02, ...
               'Callback','fm_set setpert', ...
               'Position',[0.0379*3 0.9375 0.0379 0.05], ...
               'TooltipString','Open Perturbation File', ...
               'Tag','Pushbutton8');
h1 = uicontrol('Parent',h0, ...
               'CData',fm_mat('main_settings'), ...
               'Units','normalized', ...
               'BackgroundColor',Theme.color02, ...
               'Callback','fm_setting', ...
               'Position',[0.0379*4+0.0190 0.9375 0.0379 0.05], ...
               'TooltipString','Settings', ...
               'Tag','Pushbutton4');
h1 = uicontrol('Parent',h0, ...
               'CData',fm_mat('main_save'), ...
               'Units','normalized', ...
               'BackgroundColor',Theme.color02, ...
               'Callback','fm_set savesys', ...
               'Position',[0.0379*5+0.0190 0.9375 0.0379 0.05], ...
               'TooltipString','Save System', ...
               'Tag','Pushbutton4');
h1 = uicontrol('Parent',h0, ...
               'CData',fm_mat('main_exit'), ...
               'Units','normalized', ...
               'BackgroundColor',Theme.color02, ...
               'Callback','fm_set close', ...
               'Position',[0.0379*6+0.0190 0.9375 0.0379 0.05], ...
               'TooltipString','Close window or stop process', ...
               'Tag','Pushbutton4');
h1 = uicontrol('Parent',h0, ...
               'CData',fm_mat('main_sym'), ...
               'Units','normalized', ...
               'BackgroundColor',Theme.color02, ...
               'Callback',fmlib, ...
               'Position',[0.0379*7+0.0379 0.9375 0.0379 0.05], ...
               'TooltipString','Open PSAT Simulink Library', ...
               'Tag','Pushbutton8');
h1 = uicontrol('Parent',h0, ...
               'CData',fm_mat('main_lf'), ...
               'Units','normalized', ...
               'BackgroundColor',Theme.color02, ...
               'Callback','fm_set lf', ...
               'Position',[0.0379*8+0.0569 0.9375 0.0379 0.05], ...
               'TooltipString','Solve Power Flow', ...
               'Tag','Pushbutton1');
h1 = uicontrol('Parent',h0, ...
               'CData',fm_mat('main_cpf'), ...
               'Units','normalized', ...
               'BackgroundColor',Theme.color02, ...
               'Callback','fm_cpf(''main'');', ...
               'Position',[0.0379*9+0.0569 0.9375 0.0379 0.05], ...
               'TooltipString','Continuation Power Flow', ...
               'Tag','Pushbutton4');
h1 = uicontrol('Parent',h0, ...
               'CData',fm_mat('main_opf'), ...
               'Units','normalized', ...
               'BackgroundColor',Theme.color02, ...
               'Callback','fm_set opf', ...
               'Position',[0.0379*10+0.0569 0.9375 0.0379 0.05], ...
               'TooltipString','Optimal Power Flow', ...
               'Tag','Pushbutton4');
h1 = uicontrol('Parent',h0, ...
               'CData',fm_mat('main_eigen'), ...
               'Units','normalized', ...
               'BackgroundColor',Theme.color02, ...
               'Callback','fm_eigfig', ...
               'Position',[0.0379*11+0.0759 0.9375 0.0379 0.05], ...
               'TooltipString','Eigenvalue Analysis', ...
               'Tag','Pushbutton4');
h1 = uicontrol('Parent',h0, ...
               'CData',fm_mat('main_time'), ...
               'Units','normalized', ...
               'BackgroundColor',Theme.color02, ...
               'Callback','fm_int', ...
               'Position',[0.0379*12+0.0759 0.9375 0.0379 0.05], ...
               'TooltipString','Time Domain Simulation', ...
               'Tag','Pushbutton4');
h1 = uicontrol('Parent',h0, ...
               'CData',fm_mat('main_conv'), ...
               'Units','normalized', ...
               'BackgroundColor',Theme.color02, ...
               'Callback','fm_dir(0)', ...
               'Position',[0.0379*13+0.0949 0.9375 0.0379 0.05], ...
               'TooltipString','Data Format Conversion', ...
               'Tag','Pushbutton4');
h1 = uicontrol('Parent',h0, ...
               'CData',fm_mat('main_make'), ...
               'Units','normalized', ...
               'BackgroundColor',Theme.color02, ...
               'Callback','fm_comp copen', ...
               'Position',[0.0379*14+0.0949 0.9375 0.0379 0.05], ...
               'TooltipString','Component Browser', ...
               'Tag','Pushbutton4');
h1 = uicontrol('Parent',h0, ...
               'CData',fm_mat('main_snapshot'), ...
               'Units','normalized', ...
               'BackgroundColor',Theme.color02, ...
               'Callback','fm_snap', ...
               'Position',[0.0379*15+0.0949 0.9375 0.0379 0.05], ...
               'TooltipString','Snapshots', ...
               'Tag','Pushbutton4');
h1 = uicontrol('Parent',h0, ...
               'CData',fm_mat('main_static'), ...
               'Units','normalized', ...
               'BackgroundColor',Theme.color02, ...
               'Callback','fm_stat', ...
               'Position',[0.0379*16+0.1139 0.9375 0.0379 0.05], ...
               'TooltipString','Static Report', ...
               'Tag','Pushbutton4');
h1 = uicontrol('Parent',h0, ...
               'CData',fm_mat('main_matrix'), ...
               'Units','normalized', ...
               'BackgroundColor',Theme.color02, ...
               'Callback','fm_threed', ...
               'Position',[0.0379*17+0.1139 0.9375 0.0379 0.05], ...
               'TooltipString','Sparse Matrix Visualization', ...
               'Tag','Pushbutton4');
h1 = uicontrol('Parent',h0, ...
               'CData',fm_mat('main_plot'), ...
               'Units','normalized', ...
               'BackgroundColor',Theme.color02, ...
               'Callback','fm_plotfig', ...
               'Position',[0.0379*18+0.1139 0.9375 0.0379 0.05], ...
               'TooltipString','Plotting Utilities', ...
               'Tag','Pushbutton4');
h1 = uicontrol('Parent',h0, ...
               'CData',fm_mat('main_history'), ...
               'Units','normalized', ...
               'BackgroundColor',Theme.color02, ...
               'Callback','fm_hist', ...
               'Position',[0.0379*19+0.1329 0.9375 0.0379 0.05], ...
               'TooltipString','History', ...
               'Tag','Pushbutton4');
h1 = uicontrol('Parent',h0, ...
               'CData',fm_mat('main_info'), ...
               'Units','normalized', ...
               'BackgroundColor',Theme.color02, ...
               'Callback','fm_about', ...
               'Position',[0.0379*20+0.1625 0.9375 0.0379 0.05], ...
               'TooltipString','About PSAT', ...
               'Tag','Pushbutton8');

% Message bar
h1 = uicontrol('Parent',h0, ...
               'Units','normalized', ...
               'BackgroundColor',Theme.color04, ...
               'ForegroundColor',Theme.color03, ...
               'CreateFcn','Hdl.frame = gcbo;', ...
               'Enable', 'inactive', ...
               'Position',[0.04064 0.035826 0.92118 0.043614], ...
               'Style','frame', ...
               'Tag','Frame2');
if ispc, set(h1,'Style','edit'), end
h1 = uicontrol('Parent',h0, ...
               'Units','normalized', ...
               'BackgroundColor',Theme.color04, ...
               'CreateFcn','Hdl.text = gcbo;', ...
               'Enable', 'inactive', ...
               'FontName',Theme.font01, ...
               'ForegroundColor',Theme.color05, ...
               'HorizontalAlignment','left', ...
               'Position',[0.0542 0.0400 0.8916 0.0312], ...
               'String',['PSAT version ',Settings.version, ...
		    ', Copyright (C) 2002-2013 Federico Milano'], ...
               'TooltipString','Double click for viewing the command history.', ...
               'Style','text', ...
               'Tag','StaticText13');
if nargout > 0, fig = h0; end